import {router,asyncRouterMap} from 'router';
import {getAuthMenuOperation} from 'api/api';
const menu = {
  state: {
    routers: router,
    addRouters: [],
    operation:'',
    
  },
  mutations: {
    setOperation: (state, operation) => {
      state.operation = operation;
    },
    SET_ROUTERS: (state, routers) => {
      state.addRouters = router;
      state.routers = routes.concat(router);
    },
    
  },

  actions: {
    
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        let userMenu = data.authMenu;
        let errMenuArr=[];
        userMenu=userMenu.filter((v,index) => {
          if(v.component&&v.path){
            if(v.leaf){
              let child={
                path:v.path,
                meta: {requireAuth: true} ,
                name:v.name,
                hidden:v.isFront==1?true:false,
                component:routesComponent[v.component] }
              v.children.push(child);
              v.path='/home';
              v.hidden=v.isFront==1?true:false;
              v.component=routesComponent['Home'];
            }else{
              v.component=routesComponent['Home'];
              if(v.children && v.children.length){
                v.children=v.children.filter((vC,indexs)=>{
                  if(vC.component&&vC.path){
                    vC.component=routesComponent[vC.component];
                    vC.hidden=vC.isFront==1?true:false;
                    return vC
                  }
                })
                return v;
              }
            }
            return v;
          }

        })
        // userMenu=userMenu.filter((v,index) => {
        //   if(v.component&&v.path){
        //     if(v.leaf){
        //       let child={
        //         path:v.path,
        //         meta: {requireAuth: true} ,
        //         name:v.name,
        //         component:routesComponent[v.component] }
        //       v.children.push(child);
        //       v.path='/home';
        //       v.hidden=v.isFront?true:false;
        //       v.component=routesComponent['Home'];
        //     }else{
        //       v.component=routesComponent['Home'];
        //       if(v.children && v.children.length){
        //         v.children.filter((vC,indexs)=>{
        //           if(!vC.component||!vC.path){
        //             v.children.splice(index,1)
        //           }else{
        //             v.hidden=v.isFront?true:false;
        //             vC.component=routesComponent[vC.component];
        //           }
        //         })
        //       }
        //     }
        //     return v;
        //   }
        //
        // })
        let nofound=[
          {
            path: '/404',
            component:routesComponent.NotFound,
            name: '',
            hidden: true
          },
          {
            path: '*',
            hidden: true,
            redirect: { path: '/404' }
          }
        ]
        commit('SET_ROUTERS', userMenu.concat(nofound));
        resolve();
      })
    },
    // 获取菜单操作
    getOperation({ commit },path) {
      return new Promise((resolve, reject) => {
        getAuthMenuOperation({'path':path}).then(data=>{
          if(data.obj.length){
            let Operations=''
            data.obj.filter((v,index) => {
              if(!v){
                return
              }
              let key=v['OPERATION']||v['operation']
              if(key){
                Operations+=index==data.obj.length-1?key:key+',';
              }
            })
            commit('setOperation', Operations);
          }
          resolve(data);
        }).catch(error => {
          reject(error);
        });
      });
    },
  }
};


export default menu;
